package codename.carrot.bomberman.models;

import java.util.Random;

import sheep.game.Sprite;
import sheep.graphics.Image;
import codename.carrot.bomberman.AppStorage;
import codename.carrot.bomberman.BitmapFactory;

/**
 * Class for objects of type CollectableItem. Items does not animate. 
 * @author Group A4
 * @see {@link sheep.game.Sprite} - CollectableItem directly extends Sheep's Sprite.
 */
public class CollectableItem extends Sprite {
	/**
	 * ID for items affecting player flame-attributes
	 */
    public static final int ITEM_FLAME = 0;
    /**
	 * ID for items affecting player speed-attributes
	 */
    public static final int ITEM_SPEED= 1;
    /**
	 * ID for items affecting player bomb-attributes
	 */
    public static final int ITEM_BOMB = 2;
    
    /**
     * Items start as visible
     */
    private boolean visible = true;
    
    /**
     * Item type (flame/speed/bomb)
     */
    private int type;
    /**
     * The x-coordinate of this item
     */
    private int xCoor;
    /**
     * The y-coordinate of this item
     */
    private int yCoor;
    /**
     * Positive (true) or negative (false) power-ups.
     */
    private boolean up;
        
    /**
     * Class constructor. CollectableItems are centered in their tile.
     * @param xCoor - X-coordinates where the item will be placed
     * @param yCoor - Y-coordinates where the item will be placed
     * @param image - Image used to display the item in the level 
     * @param type - item type (flame/speed/bomb)
     * @param up - Postive (true) or negative (false) power-up
     */
    private CollectableItem(int xCoor, int yCoor, Image image, int type, boolean up) {
        super(image);     
        setPosition(xCoor*AppStorage.TILE_SIZE+AppStorage.TILE_SIZE/2, yCoor*AppStorage.TILE_SIZE+AppStorage.TILE_SIZE/2);
        this.xCoor = xCoor;
        this.yCoor = yCoor;
        this.type = type;  
        this.up = up;
    }       
    
    /**
     * Getter for item type
     * @return type - the type (flame/speed/bomb) of the item
     */
    public int getType() {
        return type;
    }
    
    /**
     * Sets the item as visible
     * @param visible
     */
    public void setVisible(boolean visible) {
        this.visible = visible;
    }
    
    /**
     * Getter for the items's visibility attribute
     * @return visible - the items visibility (true/false)
     */
    public boolean isVisible() {
        return visible;
    }
    
    @Override
    /**
     * Overrides the Sheep Sprite's update function.
     */
    public void update(float dt) {
        super.update(dt);
    }
    
    /**
     * Getter for the object's X-coordinates
     * @return xCoor - the object's X-coordinates
     */
    public int getXcoorMap() {
        return xCoor;
    }
    
    /**
     * Getter for the object's Y-coordinates
     * @return yCoor - the object's Y-coordinates
     */
    public int getYcoorMap() {
        return yCoor;
    }

    /**
     * Generates a random type (flame/speed/bomb) for the item. 
     * @return type - the newly attributed item type. 
     */
    public static int getRandomType() {
        Random random = new Random();               
        
        int rnd = random.nextInt(101);
        int type = 0;
        // 33% chance for each item type. 
        if(rnd <= 33) type = ITEM_FLAME;
        else if(rnd > 33 && rnd <= 66) type = ITEM_SPEED;
        else if(rnd > 66) type = ITEM_BOMB;        
        return type;
    }
    
    /**
     * Decides whether the item should contain a positive or negative power-up
     * @param type - the type (flame/speed/bomb) of the item
     * @return isPowerUp - true for positve and false for negative power-up
     */
    public static boolean getRandomIsPowerup(int type) {
        Random random = new Random();        
        int rnd = random.nextInt(100);
        boolean isPowerUp = false;        
        if(rnd <= 70) isPowerUp = true;
        return isPowerUp;
    }
    /**
     * Getter for CollectableItem. Builds an item via the applications BitmapFactory.
     * @see {@link codename.carrot.bomberman.BitmapFactory}
     * @param xCoor - X-coordinates where the item will be spawned.
     * @param yCoor - Y-coordinates where the item will be spawned. 
     * @param type - The item's type, set by {@link codename.carrot.bomberman.models.CollectableItem#getRandomType()}
     * @param isPowerUp - Sets the item as positive or negative, set by {@link codename.carrot.bomberman.models.CollectableItem#getRandomIsPowerup(int)}
     * @return - a CollectableItem generated through the factory.
     */
    public static CollectableItem getCollectableItem(int xCoor, int yCoor, int type, boolean isPowerUp) {
        Image image = null;
        switch(type) {
        case ITEM_FLAME:
        	image = BitmapFactory.buildItemView(type, isPowerUp);
            break;
        case ITEM_BOMB:
        	image = BitmapFactory.buildItemView(type, isPowerUp);
            break;
        case ITEM_SPEED:
        	image = BitmapFactory.buildItemView(type, isPowerUp);
            break;
        }                
        return new CollectableItem(xCoor, yCoor, image, type, isPowerUp);
    }
    
    /**
     *  Returns a random collectible item
     * @param xCoor the x-coordinate for the item
     * @param yCoor the y-coordinate for the item
     * @return an item
     */
    public static CollectableItem getRandomCollectableItem(int xCoor, int yCoor) {
        int type = CollectableItem.getRandomType();
    	return getCollectableItem(xCoor, yCoor, type, CollectableItem.getRandomIsPowerup(type));
    }
    
    /**
     * Returns whether or not this is a power-up
     * @return true|false
     */
    public boolean isPowerup(){
    	return up;
    }
}
